package com.spzx.product.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.domain.*;
import com.spzx.product.mapper.*;
import com.spzx.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
   private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Autowired
    private SkuStockMapper skuStockMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Product> selectProductList(Product product) {
       return productMapper.selectProductList(product);


    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertProduct(Product product) {
        productMapper.insert(product);

        List<ProductSku> productSkuList = product.getProductSkuList();

        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(product.getId()+"_"+i);
            productSku.setProductId(product.getId());
            String skuName = product.getName() + " " + productSku.getSkuSpec();
            productSku.setSkuName(skuName);
            productSkuMapper.insert(productSku);

            SkuStock skuStock = new SkuStock();
            skuStock.setSkuId(productSku.getId());
            skuStock.setTotalNum(productSku.getStockNum());
            skuStock.setLockNum(0);
            skuStock.setAvailableNum(productSku.getStockNum());
            skuStock.setSaleNum(0);
            skuStockMapper.insert(skuStock);



        }
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(String.join(",",product.getSliderUrls()));
        productDetailsMapper.insert(productDetails);
        return 1;

    }

    @Override
    public Product selectProductById(Long id) {
        //1.查询spu
        Product product = baseMapper.selectById(id);
        //2.查询product_detail

        ProductDetails productDetails = productDetailsMapper.selectById(id);

        //3.查询spu下的所有sku列表
        List<ProductSku> skus = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>().
                eq(ProductSku::getProductId,id));

        //查询品牌名称
        Brand brand = brandMapper.selectBrandById(product.getBrandId());

        //查询1,2,3级分类标题
        HashMap<String, Long> hashMap = categoryMapper.getDetailById(product.getCategory3Id());
        ArrayList<Long> arrayList = new ArrayList<>();
            arrayList.add(hashMap.get("c1Id"));
            arrayList.add(hashMap.get("c2Id"));
            arrayList.add(hashMap.get("c3Id"));

        //4.查询sku库存
            skus.forEach(sku->{
                skuStockMapper.selectOne(new LambdaQueryWrapper<SkuStock>().eq(SkuStock::getSkuId,sku.getId()));
            });
        //5.将数据放到product中
        product.setProductSkuList(skus);
        if (productDetails!=null){
            product.setDetailsImageUrlList(Arrays.asList((productDetails.getImageUrls().split(","))));
        }

        product.setBrandName(brand.getName());
        product.setCategory1Id(arrayList.get(0));
        product.setCategory2Id(arrayList.get(1));
        product.setCategory3Id(arrayList.get(2));

        return product;
    }

    @Override
    public int updataProduct(Product product) {
        productMapper.updateById(product);
        List<ProductSku> productSkuList = product.getProductSkuList();
        //修改product_sku
        for (ProductSku productSku : productSkuList) {
            productSkuMapper.updateById(productSku);
            Long pid = productSku.getId();
            //修改sku_stock
            SkuStock skuStock = skuStockMapper.selectOne(new LambdaQueryWrapper<SkuStock>()
                    .eq(SkuStock::getSkuId,pid));
            skuStock.setTotalNum(productSku.getStockNum());
            int i = skuStock.getTotalNum() - skuStock.getLockNum();
           skuStock.setAvailableNum(i);
           skuStockMapper.updateById(skuStock);

        }


        //修改product_detail
        List<String> detailsImageUrlList = product.getDetailsImageUrlList();
        ProductDetails productDetails = productDetailsMapper.selectOne(new LambdaQueryWrapper<ProductDetails>()
                .eq(ProductDetails::getProductId, product.getId()));
        productDetails.setImageUrls(String.join(",",detailsImageUrlList));
        productDetailsMapper.updateById(productDetails);

     return 1;

    }

    @Override
    public int deleteProductByIds(Long[] ids) {
        productMapper.deleteBatchIds(Arrays.asList(ids));

        //逻辑删除detail
        productDetailsMapper.deleteByPid(ids);

        //删除sku
        for (Long id : ids) {

            //删除stock
            List<ProductSku> skus = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, id));
            for (ProductSku productSku : skus) {
                skuStockMapper.delete(new LambdaQueryWrapper<SkuStock>()
                        .eq(SkuStock::getSkuId,productSku.getId()));
            }
            productSkuMapper.delete(new LambdaQueryWrapper<ProductSku>()
                    .eq(ProductSku::getProductId,id));
        }

        return 1;
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus==1){
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        }
        else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status==1){
            product.setStatus(1);

        }else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }
}
